package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.JwBuyListBo;
import org.dromara.system.domain.vo.JwBuyListVo;
import org.dromara.system.domain.JwBuyList;
import org.dromara.system.mapper.JwBuyListMapper;
import org.dromara.system.service.IJwBuyListService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采购申请清单Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-26
 */
@RequiredArgsConstructor
@Service
public class JwBuyListServiceImpl implements IJwBuyListService {

    private final JwBuyListMapper baseMapper;

    /**
     * 查询采购申请清单
     */
    @Override
    public JwBuyListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询采购申请清单列表
     */
    @Override
    public TableDataInfo<JwBuyListVo> queryPageList(JwBuyListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JwBuyList> lqw = buildQueryWrapper(bo);
        Page<JwBuyListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采购申请清单列表
     */
    @Override
    public List<JwBuyListVo> queryList(JwBuyListBo bo) {
        LambdaQueryWrapper<JwBuyList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JwBuyList> buildQueryWrapper(JwBuyListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JwBuyList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBuyId()), JwBuyList::getBuyId, bo.getBuyId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), JwBuyList::getTitle, bo.getTitle());
        lqw.eq(bo.getUserId() != null, JwBuyList::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), JwBuyList::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), JwBuyList::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), JwBuyList::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增采购申请清单
     */
    @Override
    public Boolean insertByBo(JwBuyListBo bo) {
        JwBuyList add = MapstructUtils.convert(bo, JwBuyList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改采购申请清单
     */
    @Override
    public Boolean updateByBo(JwBuyListBo bo) {
        JwBuyList update = MapstructUtils.convert(bo, JwBuyList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JwBuyList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采购申请清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
